package org.ttjhome.server;

import java.util.Date;
import java.util.List;

import org.ttjhome.db.HunterDAO;
import org.ttjhome.db.dbo.Activity;
import org.ttjhome.db.dbo.ActivityRecord;
import org.ttjhome.db.dbo.Candidate;
import org.ttjhome.db.dbo.Company;
import org.ttjhome.db.dbo.Job;
import org.ttjhome.db.dbo.Resume;
import org.ttjhome.db.dbo.Tag;
import org.ttjhome.html.struts.common.ActivityStatus;
import org.ttjhome.html.struts.common.CandidateCriteria;
import org.ttjhome.html.struts.util.TagsCache;


public class HunterServiceImpl implements HunterService {
 

	private HunterDAO hunterDao;

	public HunterServiceImpl() {
		
		this.hunterDao = new HunterDAO();
	}

	
	public Candidate saveCandidate(Candidate candidate) {
		return hunterDao.saveOrUpdateCandidate(candidate);
	}


	
	public int getCandidateCount(CandidateCriteria criteria) {
		return hunterDao.getCandidateCount(criteria);
	}


	
	public List<Candidate> getCandidates(CandidateCriteria criteria) {
		return hunterDao.getCandidates(criteria);
	}


	
	public Candidate getCandidate(Long candidateId) {
		return hunterDao.getCandidate(candidateId);
	}



	public List<Tag> getAllTags() {
		return hunterDao.getAllTags();
	}


	public Resume getResume(String resumeId) {
		return hunterDao.getResume(resumeId);
	}
	
	
	public Tag saveTag(String tag) {
		if(TagsCache.getTag(tag)==null)
		{
			Tag newone = new Tag();
			newone.setTag(tag);
			newone = hunterDao.saveTag(newone);
			TagsCache.saveTag(newone);
			return newone;
		}else{
			return TagsCache.getTag(tag);	
		}
			
		
	}


	
	public void deleteResume(Resume resume) {
		hunterDao.deleteResume(resume);
		
	}


	public List<Company> getCompanies() {
		return hunterDao.getCompanies();
	}


	@Override
	public Company saveCompany(Company company) {
		return hunterDao.saveOrUpdateCompany(company);
	}
 

	@Override
	public Company getCompany(Long companyId) {
		return hunterDao.getCompany(companyId);
	}


	@Override
	public List<Job> getJobsByCompanyId(String companyId) {
		return hunterDao.getJobsByCompanyId(companyId);
	}


	@Override
	public Job saveJob(Job job) {
		return hunterDao.saveOrUpdateJob(job);
	}



	public Job getJob(Long id) {
		return hunterDao.getJob(id);
	}


	@Override
	public List<Activity> apply(Candidate candidate, Job job) {
		Activity activity = new Activity();
		activity.setCandidate(candidate);
		activity.setJob(job);
		activity.setStatus(ActivityStatus.InProgress.name());
		activity = hunterDao.saveOrUpdateActivity(activity);
		
		ActivityRecord record = new ActivityRecord();
		record.setActivity(activity);
		record.setRecordTime(new Date());
		record.setRecord("Apply Position in System");
		
		record = hunterDao.saveOrUpdateActivityRecord(record);
		
		//find out all the activities
		return hunterDao.getActivitiesByCandidate(candidate);
	}


	@Override
	public List<Activity> closeActivity(String activityId) {
		Activity activity = hunterDao.getActivity(Long.valueOf(activityId));
		ActivityRecord record = new ActivityRecord();
		record.setActivity(activity);
		record.setRecordTime(new Date());
		record.setRecord("Close Position in System");
		record = hunterDao.saveOrUpdateActivityRecord(record);
		
		hunterDao.closeActivity(activity);

		//find out all the activities
		return hunterDao.getActivitiesByCandidate(activity.getCandidate());
	}


	@Override
	public Activity getActivity(String activityId) {
		Activity activity = hunterDao.getActivity(Long.valueOf(activityId));
		return activity;
	}


	@Override
	public ActivityRecord saveOrUpdateActivityRecord(ActivityRecord record) {
		ActivityRecord afterRecord = hunterDao.saveOrUpdateActivityRecord(record);
		
		return afterRecord;
	}


	@Override
	public List<Activity> getActivities(Candidate candidate) {
		return hunterDao.getActivitiesByCandidate(candidate);
	}


	@Override
	public List<Tag> groupTag() {
		return hunterDao.getGroupedTag();
	}


	@Override
	public List<Activity> getActivities(Job job) {
		return hunterDao.getActivitiesByJob(job);
	}

}



